package de.pharmacontrol.psa.beans;

import javax.ejb.Singleton;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;

import de.pharmacontrol.pilot.entities.Line;
import de.pharmacontrol.pilot.entities.LineFormat;
import de.pharmacontrol.psa.beans.interfaces.LineFormatService;

@Singleton
public class LineFormatServiceBean implements LineFormatService {

	@PersistenceContext(unitName="pilot")
	protected EntityManager em;


	@Override
	public LineFormat findLineFormatByName(String formatname, Line line) {
		try {
			return em.createQuery("SELECT f FROM LineFormat f WHERE f.name = :name AND f.line = :line", LineFormat.class).setParameter("name", formatname).setParameter("line", line).getSingleResult();
		}catch(NoResultException nre) {
			return null;
		}
	}

	@Override
	public LineFormat updateLineFormat(LineFormat format) {
		return em.merge(format);
	}

	@Override
	public boolean removeLineFormat(LineFormat format) {
		if (format != null){
			em.remove(format);
			return true;
		}
		return false;
	}


}
